package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Autowired
    RoleDao roleDao;

    @Autowired
    PermissionDao permissionDao;

    @Autowired
    MenuDao menuDao;

    /*
     * 根据用户名查询用户信息(包含角色,和角色对应的权限)
     * 1.根据用户名查询用户信息
     * 2.根据用户id查询相应的角色对象
     * 3.根据角色id查询相应的权限对象
     * 4.组装数据
     *
     * */
    @Override
    public SysUser findUserByUsername(String username) {
//        1.根据用户名查询用户信息
        SysUser sysUser = userDao.findUserByUsername(username);
        if (sysUser == null) {
            return null;
        }
//        2.根据用户id查询相应的角色对象
        Set<Role> roleList = roleDao.findRoleListByUserId(sysUser.getId());
        if (roleList == null || roleList.size() == 0) {
            return sysUser;
        }

        for (Role role : roleList) {
            Set<Permission> permissionList = permissionDao.findPermissionListByRoleId(role.getId());
            role.setPermissions(permissionList);
        }
        sysUser.setRoles(roleList);
        return sysUser;
    }

    @Override
    public Integer findIdByUsername(String username) {
        return userDao.findIdByUsername(username);
    }

    @Override
    public List<Map<String,Object>> showMenuByUserId(Integer userId) {
        //1.通过userid查roleid
        Integer roleId = userDao.findRoleIdByUserId(userId);
        //2.通过roleid查menu_id集合
        List<Integer> menuIds = roleDao.findMenuIdsByRoleId(roleId);
        //3.通过menuIds批量查menu的所有数据,问题在于封装数据的格式
        List<Menu> menuList = menuDao.queryMenuByMenuId(menuIds);
        //4.创建menu主菜单集合
        List<Map<String,Object>> zhuList = new ArrayList<>();
//        // 创建Map集合
//        Map<String,Object> map1 = new LinkedHashMap<>();
////        map1.put("path", "1");
////        map1.put("title", "管理后台");
////        map1.put("icon", "fa-dashboard");
////        zhuList.add(map1);
        //5.遍历集合
        for (Menu menu : menuList) {
            //6.判断是否为子菜单  null 则不是
            if(menu.getParentMenuId() == null){
                //7.创建map集合
                Map<String,Object> map = new LinkedHashMap<>();
                //8.放数据
                map.put("path",menu.getPath());
                map.put("title",menu.getName());
                map.put("icon",menu.getIcon());

                //9.创建子菜单集合
                List<Map<String,Object>> listzi = new ArrayList<>();
                for (Menu menu1 : menuList) {
                    Map<String,Object> mapzi = new LinkedHashMap<>();
                    //10.判断子菜单id与主菜单id是否相等
                    if (menu.getId() == menu1.getParentMenuId()){
                        //11.放数据
                        mapzi.put("path",menu1.getPath());
                        mapzi.put("title",menu1.getName());
                        mapzi.put("linkUrl",menu1.getLinkUrl());
                        mapzi.put("children",new ArrayList<>());
                        listzi.add(mapzi);
                    }
                }
                //12.将子菜单集合添加到主菜单中
                map.put("children",listzi);
                zhuList.add(map);
            }
        }
        return zhuList;
    }


    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<SysUser> page=userDao.pageQuery(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(SysUser user, Integer[] roles) {
        userDao.add(user);
        Integer userId = user.getId();
        setUserAndRole(userId,roles);
    }

    @Override
    public void deleteById(Integer id) {
        long count = userDao.findCountByUserId(id);
        if (count>0){
            throw new RuntimeException(MessageConstant.USER_IS_ASSOCATION);
        }
        userDao.deleteById(id);
    }

    @Override
    public SysUser findById(Integer id) {

        return userDao.findById(id);
    }

    @Override
    public void edit(SysUser user, Integer[] roles) {
        //编辑检查组的基本信息.重新设置检查组和检查项的关联关系
        //修改基本信息
        userDao.edit(user);

        //删除检查组和检查项的关联关系

        userDao.deleteAssociationByUserId(user.getId());


        //重新设置关联关系

        this.setUserAndRole(user.getId(),roles);
    }
    public void setUserAndRole(Integer userId,Integer[] roles){
        if (roles!=null && roles.length>0){
            Map<String, Integer> map = new HashMap<>();
            for (Integer roleId : roles) {
                map.put("user_id",userId);
                map.put("role_id",roleId);
                userDao.setUserAndRole(map);
            }
        }
    }
}

